package groovy2.order;

import groovy.transform.ToString;

@ToString
class Order {
    def id = -1;
    List<OrderItem> items = new ArrayList<OrderItem>();
    
    int getTotalPrice() {
        float result = 0;
        for(OrderItem item: items) {
            result += item.price;
        }
        return result;
    }
    
    static Order createRandom(Random r) {
        Order result = new Order();
        result.id = r.nextInt();
        int count = r.nextInt(20);
        for(def i=0; i< count; i++) {
            result.items.add(OrderItem.createRandom(r));
        }
        return result;
    }
}

@ToString
class OrderItem {
    def name;
    def quantity;
    def singlePrice;
    
    float getPrice() {
        return this.singlePrice * this.quantity;
    }
    
    static OrderItem createRandom(Random r) {
        OrderItem result = new OrderItem();
        result.name = 'name-' + r.nextInt();
        result.quantity = r.nextInt(100);
        result.singlePrice = r.nextFloat() * 1000;
        return result;
    }
}


